#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json

from kpautos.engine.case import Case
from kptest.config.enum import DeviceType, HostType, DeviceUtility
from extension.test_logic.RequestBase import OpenUBMCRestful


class IPMCSET_SNMP_002(Case):
    """
            Name:
                IPMCSET_SNMP_002
            Label:
                设置SNMP trap状态
            PreCondition:
                BMC正常启动
            TestStep:
                1) 命令行执行 ipmcset -t trap -d state -v 1查询snmp trap状态
            ExpectedResult:
                A) snmp trap状态查询和web端一致
            Parameters:

            Author:
                baixin liuya
            Last Modified:

            Remark:

        """

    # 测试执行前配置等准备操作
    def pre_test_case(self):
        self.logger.info("Pre Test Case Start.... ")
        self.device_dut = self.resource.get_device(device_type=DeviceType.Server, utility=DeviceUtility.DUT)
        self.bmc_host = self.device_dut.get_host(host_type=HostType.BMC)
        self.bmc_ip = self.bmc_host.local_ip
        self.bmc_user = self.bmc_host.username
        self.bmc_password = self.bmc_host.password

        self.logger.info("初始化BMC Restful对象")
        self.bmc_restful = OpenUBMCRestful(self.bmc_ip, self.bmc_user, self.bmc_password)
        self.bmc_restful.login()

    # 测试步骤
    def procedure(self):
        self.log_step("Step1: 查询trap状态")
        resp = self.bmc_host.run({"command": ["ipmcget -t trap -d state"], "timeout": 10})
        self.logger.info(resp)

        self.log_step("Step2: 查询trap网络管理系统状态")
        trap1 = self.bmc_host.run({"command": ["ipmcget -t trap -d state -v 1"], "timeout": 10})["stdout"]
        self.logger.info(trap1)
        if "disabled" in trap1:
            trap1 = False
        else:
            trap1 = True

        trap2 = self.bmc_host.run({"command": ["ipmcget -t trap -d state -v 2"], "timeout": 10})["stdout"]
        self.logger.info(trap2)
        if "disabled" in trap2:
            trap2 = False
        else:
            trap2 = True

        trap3 = self.bmc_host.run({"command": ["ipmcget -t trap -d state -v 3"], "timeout": 10})["stdout"]
        self.logger.info(trap3)
        if "disabled" in trap3:
            trap3 = False
        else:
            trap3 = True

        trap4 = self.bmc_host.run({"command": ["ipmcget -t trap -d state -v 4"], "timeout": 10})["stdout"]
        self.logger.info(trap4)
        if "disabled" in trap4:
            trap4 = False
        else:
            trap4 = True

        self.log_step("Step3: 反向设置trap状态")
        if trap1:
            self.bmc_host.run({"command": ["ipmcset -t trap -d state -v 1 disabled"], "timeout": 10})
        else:
            self.bmc_host.run({"command": ["ipmcset -t trap -d state -v 1 enabled"], "timeout": 10})

        if trap2:
            self.bmc_host.run({"command": ["ipmcset -t trap -d state -v 2 disabled"], "timeout": 10})
        else:
            self.bmc_host.run({"command": ["ipmcset -t trap -d state -v 2 enabled"], "timeout": 10})

        if trap3:
            self.bmc_host.run({"command": ["ipmcset -t trap -d state -v 3 disabled"], "timeout": 10})
        else:
            self.bmc_host.run({"command": ["ipmcset -t trap -d state -v 3 enabled"], "timeout": 10})

        if trap4:
            self.bmc_host.run({"command": ["ipmcset -t trap -d state -v 4 disabled"], "timeout": 10})
        else:
            self.bmc_host.run({"command": ["ipmcset -t trap -d state -v 4 enabled"], "timeout": 10})

        self.log_step("Step4: 查询修改后的trap网络管理系统状态")
        new_trap1 = self.bmc_host.run({"command": ["ipmcget -t trap -d state -v 1"], "timeout": 10})["stdout"]
        new_trap2 = self.bmc_host.run({"command": ["ipmcget -t trap -d state -v 2"], "timeout": 10})["stdout"]
        new_trap3 = self.bmc_host.run({"command": ["ipmcget -t trap -d state -v 3"], "timeout": 10})["stdout"]
        new_trap4 = self.bmc_host.run({"command": ["ipmcget -t trap -d state -v 4"], "timeout": 10})["stdout"]
        self.logger.info(f"修改后的trap网络管理系统状态:\n{new_trap1}\n{new_trap2}\n{new_trap3}\n{new_trap4}")

        self.log_step("Step5: 通过RestFul查询修改后的trap网络管理系统状态")
        resp = self.bmc_restful.session.get(f"https://{self.bmc_ip}/UI/Rest/Maintenance/TrapNotification")
        content = resp.json()
        for c in content["TrapServerList"]:
            self.logger.info(f"ID: {c['ID']}_Enabled: {c['Enabled']}")

        self.log_step("Step6: 恢复trap原状态")
        if "disabled" in new_trap1:
            self.bmc_host.run({"command": ["ipmcset -t trap -d state -v 1 enabled"], "timeout": 10})
        else:
            self.bmc_host.run({"command": ["ipmcset -t trap -d state -v 1 disabled"], "timeout": 10})

        if "disabled" in new_trap2:
            self.bmc_host.run({"command": ["ipmcset -t trap -d state -v 2 enabled"], "timeout": 10})
        else:
            self.bmc_host.run({"command": ["ipmcset -t trap -d state -v 2 disabled"], "timeout": 10})

        if "disabled" in new_trap3:
            self.bmc_host.run({"command": ["ipmcset -t trap -d state -v 3 enabled"], "timeout": 10})
        else:
            self.bmc_host.run({"command": ["ipmcset -t trap -d state -v 3 disabled"], "timeout": 10})

        if "disabled" in new_trap4:
            self.bmc_host.run({"command": ["ipmcset -t trap -d state -v 4 enabled"], "timeout": 10})
        else:
            self.bmc_host.run({"command": ["ipmcset -t trap -d state -v 4 disabled"], "timeout": 10})

        self.log_step("Step7: 查询trap状态是否恢复成功")
        new_trap1 = self.bmc_host.run({"command": ["ipmcget -t trap -d state -v 1"], "timeout": 10})["stdout"]
        new_trap2 = self.bmc_host.run({"command": ["ipmcget -t trap -d state -v 2"], "timeout": 10})["stdout"]
        new_trap3 = self.bmc_host.run({"command": ["ipmcget -t trap -d state -v 3"], "timeout": 10})["stdout"]
        new_trap4 = self.bmc_host.run({"command": ["ipmcget -t trap -d state -v 4"], "timeout": 10})["stdout"]
        self.logger.info(f"{new_trap1}\n{new_trap2}\n{new_trap3}\n{new_trap4}")

        self.log_step("Step7: 通过RestFul查询trap状态是否恢复成功")
        resp = self.bmc_restful.session.get(f"https://{self.bmc_ip}/UI/Rest/Maintenance/TrapNotification")
        content = resp.json()
        for c in content["TrapServerList"]:
            self.logger.info(f"ID: {c['ID']}_Enabled: {c['Enabled']}")

    # 测试结束后恢复环境
    def post_test_case(self):
        self.bmc_restful.logout()
